#include "stdafx.h"

void Draw(void)
{
	double time; //time
	time = glfwGetTime();

	int width, height;	//get window dimensions
	glfwGetWindowSize(&width, &height);

	// avoid division by zero
	height = height < 1 ? 1 : height;

	// set viewport to the entire window
	glViewport(0, 0, width, height);

	// clear color buffer - set clear color to black
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	// clear depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// set up projection matrix
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();	// load default identity matrix

	gluPerspective(
		65.0,														// FOV = 65 degrees
		(double)width/(double)height,		// window aspect - find out a square pixel
		1.0,														// near Z clipping plane
		100.0														// far Z clipping plane
		);

	// set up modelview matrix
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();	// load default identity matrix
	gluLookAt(
		0.0, 0.0, 10.0,			// camera position (x, y, z)
		0.0, 0.0, 0.0,			// view point (x, y, z)
		0.0, 1.0, 0.0				// up vector (x, y, z)
		);

	// start actual rendering
	glBegin(GL_TRIANGLES);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-3.0f, 0.5f, -5.0f);
		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(3.0f, 0.5f, -5.0f);
		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(0.0f, -2.0f, -5.0f);
	glEnd();
}

int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << "Initializing...";

	// Initialize GLFW
	glfwInit();

	// Open openGL window
	int windowOpened = glfwOpenWindow(
		800, 600,		// width and height of window
		8, 8, 8,		// number of red, green and blue bits for color buffer
		8,					// number of bits for alpha buffer
		24,					// number of bits for depth buffer (Z-buffer)
		0,					// number of bits for stencil buffer
		GLFW_WINDOW	// windowed mode
	);

	if (!windowOpened) // we couldn't open window
	{
		glfwTerminate();
		return 0;
	}

	// everything OK, set additional params
	// Window title
	glfwSetWindowTitle("Virtual Pong 0.1");

	// enable sticky keys
	glfwEnable(GLFW_STICKY_KEYS);

	// main rendering loop
	int running;

	do
	{
		// main drawing function
		Draw();

		// we're using double buffering, swap back and front buffers
		glfwSwapBuffers();
		// check if we are still running
		running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
	}
	while (running);

	// terminate glfw
  glfwTerminate();
	// exit program
	return 0;
}